import {
  create, findMany, updateOne, deleteOne,findOne,updateStoreCount
} from '../services/material';

export default {
  namespace: 'material',
  state: {},

  effects: {
    * create({payload}, {call, put}) {
      const response = yield call(create, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            result,
          },
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * findMany({payload}, {call, put}) {
      const response = yield call(findMany, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            materials: result,
          },
        });

        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * findOne({payload}, {call, put}) {
      const response = yield call(findOne, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return response;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            material: result,
          },
        });

        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * updateOne({payload}, {call, put}) {
      const response = yield call(updateOne, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            result,
          },
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * updateStoreCount({payload}, {call, put}) {
      const response = yield call(updateStoreCount, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            result,
          },
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * deleteOne({payload}, {call, put}) {
      const response = yield call(deleteOne, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            result,
          },
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

  },

  reducers: {
    onDataLoad(state, {payload}) {
      state._notify = undefined;
      return {...state, ...payload};
    },
    onNotify(state, {payload}) {
      state._notify = payload;
      return {...state, ...payload}
    }
  },
};
